<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组示例</title>
</head>
<body>
    <h1>数组示例</h1>
    <script>
        // 示例数组
        const numbers = [1, 2, 3, 4, 5];
        console.log("数组内容:", numbers);

        // 遍历数组
        numbers.forEach((num, index) => {
            console.log(`索引 ${index}: 值 ${num}`);
        });

        // 添加新元素
        numbers.push(6);
        console.log("添加新元素后的数组:", numbers);

        // 删除最后一个元素
        numbers.pop();
        console.log("删除最后一个元素后的数组:", numbers);

        // 数组去重
        const number1 = [1,1,1,2,3,2,5,4,2,8,2,5,7];
        function removeDuplicates(arr) {
            var result = [];
            obj = {};

            for (var i in arr) {
                //判断对象属性
                if (!obj[arr[i]]) {
                    result.push(arr[i]);
                    obj[arr[i]] = 1;
                }
            }
            return result;
        }

        console.log("去重后的数组:", removeDuplicates(number1));

        // 判断一个字符串中出现次数最多的字符，统计这个次数。
        var str = "abcoefoxyozzopp";
        var json = {};
        //遍历字符串，将重复出现发字符累加
        for (var i = 0; i < str.length; i++) {
            if (!json[str.charAt(i)]) {
                json[str.charAt(i)] = 1;
            }else{
                json[str.charAt(i)]++;
            }
        }
        console.log("出现次数最多的字符:", json);

        var iMax = 0;
        var sMax = "";
        //遍历json对象找最大值
        for (var key in json) {
            if (json[key] > iMax) {
                iMax = json[key];
                sMax = key;
            }
        }
        console.log("出现次数最多的字符:", sMax);
        console.log("出现次数:", iMax);

        // 数组求和
        var arr = [1,2,3,4,5];
        function sum(arr) {
            var sum = 0;

            arr.forEach(function (value) {
                sum += value;
            })
            return sum;
        }
        console.log("数组求和:", sum(arr));

        // 浅拷贝
        var arr1 = [1,2,3,4,5];
        var arr2 = arr1;
        arr2[0] = 100;
        console.log("浅拷贝:", arr1);
        console.log("浅拷贝:", arr2);

        // 深拷贝
        var arr3 = [1,2,3,4,5]
        var arr4 = JSON.parse(JSON.stringify(arr3));

        
    </script>
</body>
</html>